<template>
	<view class="app_Cont">
		<view class="showCont" >
			<view class="formCont hasTop">
				<view class="formTitle">申办人信息</view>
				<view class="formItem">
					<view class="inputName">
					    <view class="mustIcon">*</view>
					    <view class="inputNameText">姓名</view>
				    </view>
					<view class="inputValue">
						<input maxlength="10" v-model="formInfo.real_name" placeholder="请输入真实姓名"  />
					</view>
				</view>
				<view class="formItem">
					<view class="inputName">
					    <view class="mustIcon">*</view>
					    <view class="inputNameText">手机号</view>
				    </view>
					<view class="inputValue">
						<input maxlength="11" type="number" v-model="formInfo.mobile" placeholder="请输入手机号"  />
					</view>
					<view v-if="checkPhone(formInfo.mobile)" class="iconfont icon-gouxuan1"></view>
				</view>
				<view class="formItem">
					<view class="inputName">
					    <view class="mustIcon">*</view>
					    <view class="inputNameText">身份证号</view>
				    </view>
					<view class="inputValue">
						<input maxlength="18" v-model="formInfo.id_card" placeholder="请输入身份证号"  />
					</view>
					<view v-if="checkIdCard(formInfo.id_card)" class="iconfont icon-gouxuan1"></view>
				</view>
			</view>
			<view class="formCont">
				<view class="formTitle">证书邮寄信息</view>
				<view class="formItem">
					<view class="inputName">
						<view class="mustIcon">*</view>
					    <view class="inputNameText">收货人</view>
				    </view>
					<view class="inputValue">
						<input maxlength="10"  v-model="formInfo.zs_shr" placeholder="请输入收货人姓名"  />
					</view>
				</view>
				<view class="formItem">
					<view class="inputName">
						<view class="mustIcon">*</view>
					    <view class="inputNameText">手机号</view>
				    </view>
					<view class="inputValue">
						<input maxlength="11" type="number" v-model="formInfo.zs_mobile" placeholder="请输入手机号"  />
					</view>
					<view v-if="checkPhone(formInfo.zs_mobile)" class="iconfont icon-gouxuan1"></view>
				</view>
				<view class="formItem">
					<view class="inputName">
						<view class="mustIcon">*</view>
					    <view class="inputNameText">所在地区</view>
				    </view>
					<view class="inputOut">
						<view class="inputMask" @click="showCity = true"></view>
						<input
						  v-model="formInfo.zs_city"
						  placeholder="请输入你的所属城市"
						/>
					</view>
					<view class="iconfont icon-xiala" @click="showCity = true"></view>
				</view>
				<view class="formItem">
					<view class="inputName">
						<view class="mustIcon">*</view>
					    <view class="inputNameText">详细地址</view>
				    </view>
					<view class="inputValue">
						<input maxlength="50"  v-model="formInfo.zs_address" placeholder="请输入详细地址"  />
					</view>
				</view>
			</view>
			<view class="formCont">
				<view class="formTitle">海事局——网通办平台登录密码</view>
				<view class="formItem">
					<view class="inputValue">
						<input maxlength="50"  v-model="formInfo.hsj_password" placeholder="请输入平台登录密码"  />
					</view>
				</view>
				<view class="formTip">温馨提示：鲸领航不会泄露您的账号、密码信息。此信息仅用于鲸领航为您进行证书申办使用。若未在海事局一网通办平台进行注册，请填写[海事局综合服务平台]登录密码。</view>
			</view>
			<view class="formCont ">
				<view class="formTitle">备注</view>
				<view class="formItem noPadd">
					<textarea v-model="formInfo.remark" placeholder="请输入备注" ></textarea>
				</view>
			</view>
			<view class="linkCont">
				<view @click="isCheck = !isCheck" :class="isCheck?'checkBox isCheck iconfont icon-gouxuan1':'checkBox iconfont icon-gouxuan1' "></view>
				<view>我已阅读并同意<text class="linkText">《用户协议》</text> 和<text class="linkText">《隐私政策》</text></view>
			</view>
			<view class="saveBtn" @click="saveFn">立即办理（合计：¥{{orderMoney}}）</view>
		</view>
		<u-notify ref="uNotify"></u-notify>
		<subLoad v-if="showSub"></subLoad>
		
		<u-modal :show="showModal" confirmText="查看订单" :showCancelButton="true" @cancel="showModal = false" title="成功提示" @confirm="modalSure"  :content='modalCont'></u-modal>
		<u-picker :show="showCity" ref="cityPicker" :columns="cityArr" @confirm="citySureFn" @change="changeCityFn" @cancel="showCity = false" @close="showCity = false" :closeOnClickOverlay="true"></u-picker>
	</view>
</template>
<script>
	
	import subLoad from '@/components/sub.vue'
	import api from '@/http/api.js'
	import city from "@/utils/city";
	import ruleCheck from '@/utils/ruleCheck.js'
	export default {
		components:{subLoad},
		data() {
			return {
				
				showSub:false,
				showCity:false,
				detailId:'',
				sign_id:'',
				orderMoney:0,
				isCheck:false,
				cityArr:city.getCity().parentCity,
				childCityArr:city.getCity().childCity,
				formInfo:{
					accreditation_content_id:'',
					real_name:'',
					mobile:'',
					id_card:'',
					zs_shr:'',
					zs_mobile:'',
					zs_city:'',
					zs_address:'',
					hsj_password:'',
					remark:'',
				},
				modalCont:'',
				showModal:false,
			};
		},
		methods:{
			checkPhone(number){
				return !ruleCheck.checkPhone(number)
			},
			checkIdCard(number){
				return !ruleCheck.checkIdCard(number)
			},
			checkRule(data){
				let tipData = {
					bgColor: '#e47470',
					message: '',
					top:'0',
				}
				if(!data.real_name){
					tipData.message = '真实姓名不能为空'
					this.$refs.uNotify.show(tipData)
					return false
				}else if(ruleCheck.checkPhone(data.mobile)){
					tipData.message = ruleCheck.checkPhone(data.mobile)
					this.$refs.uNotify.show(tipData)
					return false
				}else if(ruleCheck.checkIdCard(data.id_card)){
					tipData.message = ruleCheck.checkIdCard(data.id_card)
					this.$refs.uNotify.show(tipData)
					return false
				}else if(!data.zs_shr){
					tipData.message = '收货人不能为空'
					this.$refs.uNotify.show(tipData)
					return false
				}else if(ruleCheck.checkPhone(data.zs_mobile)){
					tipData.message = ruleCheck.checkPhone(data.zs_mobile)
					this.$refs.uNotify.show(tipData)
					return false
				}else if(!data.zs_city){
					tipData.message = '收货所在地区不能为空'
					this.$refs.uNotify.show(tipData)
					return false
				}else if(!data.zs_address){
					tipData.message = '收货详细地址不能为空'
					this.$refs.uNotify.show(tipData)
					return false
				}else if(!this.isCheck){
					tipData.message = '认真阅读并勾选上方协议'
					this.$refs.uNotify.show(tipData)
					return false
				} else{
					return true
				}
			},
			modalSure(){
				uni.redirectTo({
					url: '/pages/user/obtainRecord/obtainRecordDetail/obtainRecordDetail?id='+this.sign_id
				});
			},
			saveFn(){
				let result = this.checkRule(this.formInfo)
				if(result){
				   this.showSub = true
				   api.getRequst(this.formInfo,'/api/accreditation/sign').then((res)=>{
					if(res.code==1){
						this.sign_id = res.data.sign_id
						this.payFn(res.data)
					}
					this.showSub =false
				   })
				}
			},
			payFn(payRes){
				uni.requestPayment({
					"provider": "wxpay",//支付方式  
					"timeStamp":payRes.wx_pay.timeStamp,//时间戳
					"nonceStr":payRes.wx_pay.nonceStr,//随机字符串
					"package":payRes.wx_pay.package,//接口返回的prepay_id
					"signType":payRes.wx_pay.signType,//签名算法，需要与后台下单时一致
					"paySign":payRes.wx_pay.paySign,
					success: (resSuccess)=> {
						this.showModal = true
						this.modalCont = '支付成功，等待办证员接单～'
						//触发该success时已经是输入密码支付成功后触发，可做一些页面跳转和成功提示                        
					 },				
					fail: function (err) {
						//支付失败时出发，比如取消支付、余额不足等
					}
				})
			},
			citySureFn(data){
				this.formInfo.zs_city = data.value.join('-')
				this.showCity = false
			},
			changeCityFn(e){
				const {
					columnIndex,
					value,
					values, // values为当前变化列的数组内容
					index,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
					picker = this.$refs.cityPicker
				} = e
				// 当第一列值发生变化时，变化第二列(后一列)对应的选项
				if (columnIndex === 0) {
					// picker为选择器this实例，变化第二列对应的选项
					picker.setColumnValues(1, this.childCityArr[index])
				}
			},
		},
		onLoad(option) {
			this.detailId = option.id
			this.formInfo.accreditation_content_id = option.id
			this.orderMoney = option.money
			
		}
	}
</script>

<style lang="scss">
.showCont{
	min-height: 100vh;
	background-color: #f8f8f8;
	box-sizing: border-box;
	padding-bottom: 30rpx;
	padding-top: 30rpx;
	.formCont{
		background-color: #fff;
		border-radius: 30rpx;
		padding: 40rpx 30rpx 60rpx;
		width: 92%;
		margin: 0 auto 30rpx;
		box-sizing: border-box;
		.formTitle{
			font-size: 32rpx;
			margin-bottom: 30rpx;
			font-weight: bold;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.addBtn{
				font-size: 24rpx;
				font-weight: normal;
				color: #3f55cc;
			}
		}
		.formItem{
			margin-top: 30rpx;
			display: flex;
			align-items: center;
			background-color: #f8f8f8;
			padding: 25rpx 50rpx;
			box-sizing: border-box;
			border-radius: 50rpx;
			.inputName {
			  width: 140rpx;
			  position: relative;
			  font-size: 28rpx;
			  .mustIcon {
				font-size: 28rpx;
				top: 0;
				left: -16rpx;
				position: absolute;
				color: #ff5757;
			  }
			}
			.inputOut {
				flex: 1;
				width: 3rem;
				position: relative;
				.inputMask {
					width: 100%;
					height: 100%;
					position: absolute;
					left: 0;
					top: 0;
					z-index: 2;
				  }
				  input{
					width: 100%;
					border: none;
					background: none;
					outline: none;
					position: relative;
					z-index: 1;
				  }
			}
			.inputValue{
				flex: 1;
				width: 100rpx;
			}
			.getCode{
				font-size: 24rpx;
				color: #3f55cc;
			}
			.icon-gouxuan1,.icon-xiala{
				color: #3f55cc;
			}
		}
		.noPadd{
			padding: 0;
		}
		.formTip{
			font-size: 24rpx;
			color: #999;
			margin-top: 10rpx;
		}

		
		.formDesItem{
			display: flex;
			margin-bottom: 20rpx;
			.desName{
				color: #333;
				width: 120rpx;
				text-align: right;
				margin-right: 30rpx;
			}
		}
		.formMustRead{
			text-indent: 60rpx;
			line-height: 40rpx;
		}
	}
	.cardItem{
		width: 92%;
		margin: 30rpx auto;
		background-color: #fff;
		padding: 30rpx 4%;
		box-sizing: border-box;
		border-radius: 20rpx;
		.cardHead{
			font-size: 30rpx;
			font-weight: bold;
			margin-bottom: 20rpx;
		}
		.cardForm{
			border-radius: 20rpx;
			overflow: hidden;
			.formItem{
				display: flex;
				align-items: center;
				font-size: 24rpx;
				background-color: #d6e0fb;
				.formName{
					padding:30rpx 20rpx;
					width: 140rpx;
					box-sizing: border-box;
				}
				
				.formText{
					flex: 1;
					padding:30rpx 20rpx;
					width: 100rpx;
					background-color: #ebf1fb;
					.formTextDes{
						color: #999;
					}
				}
				.formTextPalin{
					background-color: #dee3f8;
				}
			}
			.formItemPlain{
				background-color: #cbd3fa;
			}
		}
		.formDesMoney{
			display: flex;
			justify-content: flex-end;
			padding-top: 30rpx;
			align-items: center;
			.redText{
				color: #ff5757;
				font-size: 40rpx;
			}
		}
	}
	.linkCont{
		width:100%;
		padding: 0 4%;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		color: #666;
		.checkBox{
			width: 24rpx;
			height: 24rpx;
			font-size: 24rpx;
			border-radius: 4rpx;
			border: 1px solid #ddd;
			background-color: #fff;
			color: #fff;
			text-align: center;
			line-height: 24rpx;
			margin-right: 15rpx;
		}
		.isCheck{
			background-color: #3f55cc;
			border: 1px solid #3f55cc;
		}
		.linkText{
			color: #3f55cc;
		}
	}
	.noBootm{
		padding-bottom: 30rpx;
	}
	.saveBtn{
		width: 92%;
		margin: 30rpx auto 0;
		border-radius: 50rpx;
		background-image: linear-gradient(to right,#9ecffd,#3f55cc);
		color: #fff;
		font-size: 24rpx;
		padding: 30rpx 0;
		text-align: center;
		z-index: 2;
	}
}
</style>
